package com.origin.uilibrary;


import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 长按拦截
 */
public class LongClickLinearLayout extends RelativeLayout {
    public final static String TAG = "LongClickLinearLayout";
    int mTouchSlop; //最短滑动距离

    public LongClickLinearLayout(Context context) {
        this(context, null);
    }

    public LongClickLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LongClickLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public LongClickLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    MyHandler handler;

    private void init(Context context) {
        handler = new MyHandler();
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        Log.e(TAG, " mTouchSlop " + mTouchSlop);
    }


    private boolean isLongClick = false; //是否是长点击事件
    //    private boolean isRelease = false; //是否已经释放
    private static int LONG_CLICK_TIME = 600;

    private LongClickListener longClickListener;

    //自定义长点击事件接口
    public interface LongClickListener {
        void OnLongClick();
    }

    public void setLongClickListener(LongClickListener l) {
        this.longClickListener = l;
    }

    private final Runnable countDownRunnable = new Runnable() {
        @Override
        public void run() {
            isLongClick = true;
            //当用户在LONG_CLICK_TIME时间内没有做抬起滑动等取消动作，则触发longclick事件
            Log.e(TAG, "countDownRunnable 1");
//            if (!isRelease) {
//                return;
//            }
//            isRelease = true;
            Log.e(TAG, "countDownRunnable 2");
            post(() -> {

                if (longClickListener != null) {
                    performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING
                    );
                    longClickListener.OnLongClick();
                }
            });
        }
    };


    //记录按下时的坐标
    int downX = 0;
    int downY = 0;

    private static class MyHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "down");
                downX = (int) event.getX();
                downY = (int) event.getY();
//                isRelease = false;
                isLongClick = false;
                //延迟LONG_CLICK_TIME毫秒的时间，触发长点击事件
                handler.postDelayed(countDownRunnable, LONG_CLICK_TIME);
                break;
            case MotionEvent.ACTION_MOVE:

                float mx = Math.abs(event.getX() - downX);
                float my = Math.abs(event.getX() - downX);
                Log.e(TAG, "mx " + mx + " my " + my + "  isRelease  ");
                //当横移或纵移的长度大于系统规定的滑动最短距离时，则视为用户取消了longclick事件
                if (Math.abs(event.getX() - downX) > mTouchSlop || Math.abs(event.getY() - downY) > mTouchSlop) {
                    Log.e(TAG, "move");
                    handler.removeCallbacks(countDownRunnable);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                Log.e(TAG, "取消  " + event.getAction());
                handler.removeCallbacks(countDownRunnable);
                if (isLongClick) {
                    //当已经是longclick事件时，parent则拦截该事件，child不会再收到该事件
                    return true;
                }
                break;
        }

        boolean isDispatch = super.dispatchTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_DOWN && !isDispatch) {
            //当down事件返回false时 不触发up事件 所以返回true强制触发UP事件，否则会出现click父布局出现longclick的效果
            return true;
        }
        return isDispatch;
    }

}
